package
{
	import com.evola.rm.model.Alias;
	import com.evola.rm.model.Language;
	import com.evola.rm.model.Project;
	import com.evola.rm.utils.XMLConvertor;
	import com.evola.rm.view.alias.AliasEditorPopup;
	import com.evola.rm.view.newprojectwizard.NewProjectWizardView;
	import com.evola.rm.view.project.ProjectModel;
	import com.evola.rm.view.project.ProjectView;
	
	import flash.events.Event;
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	import flash.net.FileFilter;
	
	import mx.controls.Alert;
	import mx.core.Application;
	import mx.managers.PopUpManager;

	[Bindable]
	public class MainPresenter
	{
		
		public static var instance : MainPresenter = new MainPresenter();
		
		public var model : MainModel = MainModel.instance;
		public var view : EvolaResourceManager;
		
		public function MainPresenter()
		{
		}
		
		public function newProject() : void{
			
			var wizard : NewProjectWizardView = new NewProjectWizardView();
			
		   PopUpManager.addPopUp(wizard, view, true);
		   PopUpManager.centerPopUp(wizard);
		   
		   wizard.init();
		}
		
		public function openProject():void
		{

			var file:File=new File();
			var fileFilter:FileFilter=new FileFilter("Evola Resource Manager project file (*.rmp)", "*.rmp");

			file.addEventListener(Event.SELECT, onFileSelected);
			file.browseForOpen("Select project file 'rmp'", [fileFilter]);
		}
		
		private function onFileSelected(event:Event):void
		{
			
			(event.target as File).removeEventListener(Event.SELECT, onFileSelected);
			
			var stream:FileStream = new FileStream();
			stream.open(event.target as File, FileMode.READ);
			var fileData:String = stream.readUTFBytes(stream.bytesAvailable);
			
			var xml : XML = new XML(fileData);
			
			addProject(XMLConvertor.createFromXML(xml), event.target as File);
		}
		
		public function openNewProject(newProjectFile : File) : void{
			
			var stream:FileStream = new FileStream();
			stream.open(newProjectFile, FileMode.READ);
			var fileData:String = stream.readUTFBytes(stream.bytesAvailable);
			
			var xml : XML = new XML(fileData);
			
			addProject(XMLConvertor.createFromXML(xml), newProjectFile);
		}
		
		public function addProject(param0:Project, projectFile : File):void
		{
			
			//pravimo i novi project view
			var pv : ProjectView = new ProjectView();
			pv.presenter.setup(param0, projectFile);
			
			model.projectsViews.addItem(pv);
			
			view.tn.addChild(pv);
			view.tn.selectedChild = pv;
		}
		
		public function saveProject() : void{
			
			if(!model.selectedProjectView){
				
				Alert.show("Select project first.");
				return;
			}
			
			//cuvanje projekta znaci cuvanje svih jezika i cuvanje konfiguracionog fajla
			//prvo cuvamo konfiguraciju projekta
			var projectModel : ProjectModel = model.selectedProjectView.presenter.model;
			
			var projectXml : XML = XMLConvertor.exportToXml(projectModel.project);
			
			//trace(projectXml.toXMLString());
			
			var fileStream : FileStream = new FileStream();
			fileStream.openAsync(projectModel.projectFile, FileMode.WRITE);
			
			fileStream.writeUTFBytes(projectXml);
			fileStream.close();
			
			var langText : String;
			//sada treba da pisemo fajlove jezika
			for each(var lang : Language in projectModel.project.languages){
				
				langText = lang.toPropertiesText();
				
				var langFile : File = new File(lang.path);
				var langStream : FileStream = new  FileStream();
				langStream.openAsync(langFile, FileMode.WRITE);
				
				langStream.writeUTFBytes(langText);
				langStream.close();
				
				//trace("LANGUAGE "+lang.code+":\n"+langText);
			}
			
			projectModel.projectUpdated = false;
		}
		
		public function openAliases() : void{
			
			if(!model.selectedProjectView){
				
				Alert.show("Select project first.");
				return;
			}
			
			var popup : AliasEditorPopup = new AliasEditorPopup();
			popup.presenter = model.selectedProjectView.presenter;
			
			PopUpManager.addPopUp(popup, view, true);
			PopUpManager.centerPopUp(popup);
		}
		
		public function getApplicationTitle(projectView : ProjectView) : String{
			
			var title : String = "";
			
			if(model.selectedProjectView){
				
				title += model.selectedProjectView.presenter.model.project.name;
				
				if(model.selectedProjectView.presenter.model.projectUpdated)
					title += "*";

				title += " - ";
			}

			title += "Evola Resource Manager";
			
			return title;
		}
	}
}